package com.itbuka.goods.controller;

import com.itbuka.goods.domain.GoodsBrand;
import com.itbuka.entity.StatusCode;
import com.itbuka.goods.service.GoodsBrandService;
import com.itbuka.utils.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.itbuka.entity.Result;
import com.itbuka.entity.PageResult;
import com.github.pagehelper.Page;

import java.util.List;
import java.util.Map;

/**
 * 品牌表
 */
@RestController
@RequestMapping("/GoodsBrand")
public class GoodsBrandController {

    @Autowired
    private TokenDecode tokenDecode;


    @Autowired
    private GoodsBrandService goodsBrandService;

    /**
     * 查询全部品牌表
     */
    @GetMapping("/selectAll")
    @PreAuthorize("hasAnyAuthority('a')")
    public Result<List<GoodsBrand>> selectAll() {
        String username = tokenDecode.getUserInfo().get("username");
        return Result.ok("查询成功", goodsBrandService.selectAll());
    }

    /**
     * 条件查询品牌表
     */
    @PostMapping("/selectList")
    public Result<List<GoodsBrand>> select(@RequestBody GoodsBrand goodsBrand) {
        return Result.ok("条件查询成功", goodsBrandService.selectList(goodsBrand));
    }

    /**
     * 新增品牌表
     */
    @PostMapping("/insert")
    public Result<?> insert(@RequestBody GoodsBrand goodsBrand) {
        goodsBrandService.insert(goodsBrand);
        return Result.ok("新增成功");
    }

    /**
     * 删除品牌表
     */
    @GetMapping("/delete")
    public Result<?> delete(@RequestParam String ids) {
        goodsBrandService.delete(ids);
        return Result.ok("删除成功");
    }

    /**
     * 修改品牌表
     */
    @PostMapping("/update")
    public Result<?> update(@RequestBody GoodsBrand goodsBrand) {
        goodsBrandService.update(goodsBrand);
        return Result.ok("修改成功");
    }

    /**
     * 分页搜索实现品牌表
     */
    @GetMapping(value = "/pageAll")
    public Result<?> pageAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<GoodsBrand> pageList = goodsBrandService.pageAll(page, size);
        PageResult<?> pageResult = new PageResult<>(pageList.getTotal(), pageList.getResult());
        return Result.ok("查询成功", pageResult);
    }

    /**
     * 分页条件搜索实现品牌表
     */
    @GetMapping(value = "/pageList")
    public Result<?> pageList(@RequestBody GoodsBrand goodsBrand, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<GoodsBrand> pageList = goodsBrandService.pageList(goodsBrand, page, size);
        PageResult<?> pageResult = new PageResult<>(pageList.getTotal(), pageList.getResult());
        return Result.ok("查询成功", pageResult);
    }

    /**
     * 品牌启用、停用
     */
    @GetMapping("/status")
    public Result<?> status(@RequestParam Long id, @RequestParam Integer status) {
        if (id == null) {
            return new Result<>(false, StatusCode.ERROR, "查询不到品牌");
        }
        Integer integer = null;
        if (status == null) {
            integer = goodsBrandService.status(id);
        } else {
            integer = goodsBrandService.status(id, status);
        }
        if (integer != 1) {
            return new Result<>(false, StatusCode.ERROR, "品牌状态修改失败");
        }
        return new Result<>(true, StatusCode.OK, "品牌状态修改成功");
    }

    /**
     * 根据分类名称查询品牌列表
     */
    @GetMapping("/category")
    public Result<?> findListByCategoryName(@RequestParam String category) {
        System.out.println(category);
        List<Map<?, ?>> brandList = goodsBrandService.findListByCategoryName(category);
        return Result.ok("查询成功", brandList);
    }
}




